#include <iostream>
#include <malloc.h>
using namespace std;

int main(int argc, char const *argv[])
{
    // C语言的动态内存分配
    // 1.申请内存
    int *p = (int *)malloc(sizeof(int));

    int *pArray = (int *)malloc(sizeof(int) * 10);

    // 2.使用内存
    *p = 10;
    for (int i = 0; i < 10; i++)
    {
        pArray[i] = i;
    }
    cout << "*p = " << *p << endl;
    for (int i = 0; i < 10; i++)
    {
        cout << "pArray[" << i << "]= " << pArray[i] << " ";
    }
    cout << endl;
    // 3.释放内存
    free(p);
    free(pArray);

    // C++的动态内存分配
    // 1.申请内存
    int *p1 = new int;
    int *p2 = new int(10);
    int *pArray1 = new int[10];

    // 2.使用内存
    *p1 = 10;
    for (int i = 0; i < 10; i++)
    {
        pArray1[i] = i;
    }
    cout << "*p1 = " << *p1 << endl;
    cout << "*p2 = " << *p2 << endl;
    for (int i = 0; i < 10; i++)
    {
        cout << "pArray1[" << i << "]= " << pArray1[i] << " ";
    }
    cout << endl;
    // 3.释放内存
    delete p1;
    delete p2;
    delete[] pArray1;



    return 0;
}
